import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

const routes: Array<RouteRecordRaw> = [
	{
		path: "/",
		redirect: "/home",
	},
	{
		path: "/home",
		name: "Home",
		component: () => import("@/views/home/index.vue"),
		meta: {
			title: "首页",
			keepAlive: true,
		},
	},
	{
		path: "/login",
		name: "Login",
		component: () => import("@/views/login/index.vue"),
		meta: {
			title: "登录",
			showTabBar: false,
		},
	},
	{
		path: "/product",
		name: "Product",
		component: () => import("@/views/product/index.vue"),
		meta: {
			title: "商品列表",
			keepAlive: true,
		},
	},
	{
		path: "/product/:id",
		name: "ProductDetail",
		component: () => import("@/views/product/detail.vue"),
		meta: {
			title: "商品详情",
			showTabBar: false,
		},
	},
	{
		path: "/cart",
		name: "Cart",
		component: () => import("@/views/cart/index.vue"),
		meta: {
			title: "购物车",
			requiresAuth: true,
		},
	},
	{
		path: "/order",
		name: "Order",
		component: () => import("@/views/order/index.vue"),
		meta: {
			title: "我的订单",
			requiresAuth: true,
		},
	},
	{
		path: "/order/create",
		name: "OrderCreate",
		component: () => import("@/views/order/create.vue"),
		meta: {
			title: "创建订单",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	{
		path: "/order/:id",
		name: "OrderDetail",
		component: () => import("@/views/order/detail.vue"),
		meta: {
			title: "订单详情",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	{
		path: "/address",
		name: "Address",
		component: () => import("@/views/address/index.vue"),
		meta: {
			title: "地址管理",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	{
		path: "/address/edit",
		name: "AddressEdit",
		component: () => import("@/views/address/edit.vue"),
		meta: {
			title: "编辑地址",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	{
		path: "/address/list",
		name: "AddressList",
		component: () => import("@/views/address/list.vue"),
		meta: {
			title: "我的地址",
			showTabBar: false,
		},
	},
	{
		path: "/mine",
		name: "Mine",
		component: () => import("@/views/mine/index.vue"),
		meta: {
			title: "个人中心",
			requiresAuth: true,
		},
	},
	// 个人信息页面
	{
		path: "/mine/profile",
		name: "Profile",
		component: () => import("@/views/mine/profile.vue"),
		meta: {
			title: "个人信息",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	// 积分明细页面
	{
		path: "/mine/points",
		name: "Points",
		component: () => import("@/views/mine/points.vue"),
		meta: {
			title: "积分明细",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	// 修改密码页面
	{
		path: "/mine/password",
		component: () => import("@/views/mine/password.vue"),
		meta: {
			title: "修改密码",
			showTabBar: false,
			requiresAuth: true,
		},
	},
	// 404 页面
	{
		path: "/:pathMatch(.*)*",
		name: "NotFound",
		component: () => import("@/views/error/404.vue"),
		meta: {
			title: "页面不存在",
			showTabBar: false,
		},
	},
	// 添加注册路由
	{
		path: "/register",
		name: "Register",
		component: () => import("@/views/register/index.vue"),
		meta: {
			title: "用户注册",
			showTabBar: false,
		},
	},
];

const router = createRouter({
	history: createWebHistory(),
	routes,
});

// 全局前置守卫
router.beforeEach((to, _, next) => {
	// 设置标题
	document.title = (to.meta.title as string) || "积分商城";

	// 检查是否需要登录权限
	if (to.meta.requiresAuth) {
		const token = localStorage.getItem("token");
		if (!token) {
			// 没有token，跳转到登录页
			next({
				path: "/login",
				query: { redirect: to.fullPath },
			});
			return;
		}
	}

	next();
});

export default router;
